package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.management.GcInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Override

    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        super.saveOrUpdate(roomSubmitVo);
        if (roomSubmitVo.getId() != null) {
            //删除图片
            LambdaQueryWrapper<GraphInfo> eq = new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, roomSubmitVo.getId()).eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(eq);
            //删除房间信息属性
            LambdaQueryWrapper<RoomAttrValue> eq1 = new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(eq1);
            //删除房间标签
            LambdaQueryWrapper<RoomLabel> eq2 = new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(eq2);
            //删除配套
            LambdaQueryWrapper<RoomFacility> eq3 = new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(eq3);
            //删除支付方式
            LambdaQueryWrapper<RoomPaymentType> eq4 = new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(eq4);
            //删除可选租期
            LambdaQueryWrapper<RoomLeaseTerm> eq5 = new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(eq5);
            //执行删除并更新
        }
        //添加图像
        roomSubmitVo.getGraphVoList().forEach(graphVo -> {
            GraphInfo build = GraphInfo.builder()
                    .name(graphVo.getName())
                    .url(graphVo.getUrl()).itemId(roomSubmitVo.getId()).itemType(ItemType.ROOM).build();
            graphInfoService.saveOrUpdate(build);
        });
        /* 添加房间属性 */
        roomSubmitVo.getAttrValueIds().forEach(attrValueId -> {
            RoomAttrValue build = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
            roomAttrValueService.saveOrUpdate(build);

        });
        //添加配套
        roomSubmitVo.getFacilityInfoIds().forEach(facilityInfoId -> {
            RoomFacility build = RoomFacility.builder().facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();
            roomFacilityService.saveOrUpdate(build);
        });
        //添加标签
        roomSubmitVo.getLabelInfoIds().forEach(labelInfoId -> {
            RoomLabel build = RoomLabel.builder().labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
            roomLabelService.saveOrUpdate(build);
        });
        //添加支付方式
        roomSubmitVo.getPaymentTypeIds().forEach(paymentTypeId -> {
            RoomPaymentType build = RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
            roomPaymentTypeService.saveOrUpdate(build);
        });
//添加可选租期
        roomSubmitVo.getLeaseTermIds().forEach(leaseTermId -> {
            RoomLeaseTerm build = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build();
            roomLeaseTermService.saveOrUpdate(build);

        });
    }

    @Override
    public IPage<RoomItemVo> selectPageVo(IPage<RoomItemVo> objectPage, RoomQueryVo queryVo) {
        return roomInfoMapper.selectPageVo(objectPage, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailVo(Long id) {
        //先进行基本信息查询
        RoomInfo byId = super.getById(id);//公寓信息
        if (byId == null) {
            return null;
        }
        ApartmentInfo apartmentInfo = apartmentInfoService.selectById(byId.getApartmentId());
        List<GraphVo> graphVoList = graphInfoService.getRoomGraph(id);
        List<AttrValueVo> attrValueVoList = roomAttrValueService.getAttrValueVo(id);//公寓属性查询
        List<FacilityInfo> facilityInfoList = roomFacilityService.selectListByRoomId(id);
        List<LabelInfo> labelInfoList = roomLabelService.selectListByRoomId(id);
        List<PaymentType> paymentTypeList = roomPaymentTypeService.selectListByRoomId(id);
        List<LeaseTerm> leaseTermList = roomLeaseTermService.selectListByRoomId(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(byId, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        super.removeById(id);

        //删除图片信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
        //删除标签信息
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        //删除配套信息
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        //删除支付方式
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
//删除租约信息
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        //删除属性信息
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));


    }


}




